﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MMDemo
{
    public partial class BinTreeHoffman : Form
    {
        public BinTreeHoffman()
        {
            InitializeComponent();
        }

        private int Hoffnum = 0;
        private string[] hoffdata; 
        private void RamCrebtn_Click(object sender, EventArgs e)
        {
            InputStrtext.Text = "";
            Random ran = new Random();
            Hoffnum = ran.Next(2, 11);
            for (int i = 0; i < Hoffnum;++i )
            {
                if (i != 0)
                {
                    InputStrtext.Text += ",";
                }
                InputStrtext.Text += ran.Next(1, 20);                
            }
        }

        private void Okbtn_Click(object sender, EventArgs e)
        {
            String temp = InputStrtext.Text;
            hoffdata = temp.Split(',');
            try
            {
                if (hoffdata.Length >= 11 || hoffdata.Length < 2)
                {
                    MessageBox.Show("获得较佳的演示效果，请输入2-10个权值!");
                    return;
                }
                for (int i = 0; i < hoffdata.Length; ++i)
                {
                    if (Convert.ToUInt32(hoffdata[i]) >= 20 || Convert.ToUInt32(hoffdata[i]) < 1)
                    {
                        MessageBox.Show("获得较佳的演示效果，权值大小为1-19!");
                        return;
                    }                    
                }
            }
            catch
            {
                MessageBox.Show("输入的权值有误!");
                return;
            }            
            GlobalBinTree.HoffmanData = hoffdata;
            PublicDataClass.IsInit = true;
            this.Close();
        }
        protected override void WndProc(ref   Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_CLOSE = 0xF060;
            if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
            {
                PublicDataClass.IsInit = false;
                this.Close();
                return;
            }
            base.WndProc(ref m);
        }
    }
}
